/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.thb.pay.core.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //是否开启swagger，正式环境一般是需要关闭的，可根据springboot的多环境配置进行设置
    @Value(value = "${swagger.enabled:true}")
    private Boolean swaggerEnabled;

    /**
     * swagger API 扫描类型 1 路径扫描、2 注解扫描
     */
    @Value(value = "${swagger.scanApiType:1}")
    private Integer scanApiType;

    @Value(value = "${swagger.scanApiPath:com.thb.pay")
    private String scanApiPath;

    @Bean
    public Docket createRestApi() {
        ParameterBuilder tokenBuilder = new ParameterBuilder();
        List<Parameter> parameterList = new ArrayList<Parameter>();
        tokenBuilder.name("Authorization")
                .defaultValue("去其他请求中获取heard中token参数")
                .description("令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true).build();
        parameterList.add(tokenBuilder.build());
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.apiInfo(apiInfo())
                .enable(swaggerEnabled);

        ApiSelectorBuilder apiSelectorBuilder = docket.select();
        if (scanApiType == 2) {
            apiSelectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
        } else {
            apiSelectorBuilder.apis(RequestHandlerSelectors.basePackage(scanApiPath));
        }
        apiSelectorBuilder.paths(PathSelectors.any());

        return apiSelectorBuilder.build()
                .globalOperationParameters(parameterList);

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("thb platform Swagger API ")
                .description("https://gitee.com/getThingsDone/thb-platform/wikis")
                .termsOfServiceUrl("https://gitee.com/getThingsDone/thb-platform")
                .contact(new Contact("lucky","https://gitee.com/getThingsDone/thb-platform","771385381@qq.com"))
                .version("1.0")
                .build();
    }

}